//
//  MHProfileViewModel.m
// MiaoCircle
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHProfileViewModel.h"
#import "MHUserInfoViewModel.h"
#import "MHSettingViewModel.h"
#import "MHShareAppViewModel.h"
#import "MHHelperViewModel.h"
#import "MHAccountSecurityViewModel.h"
#import "MHFeedBackViewModel.h"
#import "MHMyWalletViewModel.h"
#import "MHAboutUsViewModel.h"
//#import "MHEmotionViewModel.h"
#import "MHBindInvitedCodeViewModel.h"

#if defined(DEBUG)||defined(_DEBUG)
/// PS：调试模式，这里在ViewModel中引用了UIKite的东西， 但是release模式下无效，这里只是用作测试而已
#import "MHDebugTouchView.h"
#endif

@interface MHProfileViewModel()
/// The current `user`.
@property (nonatomic, readwrite , strong) MHUser *user;

@end


@implementation MHProfileViewModel

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        /// 获取user
        self.user = [[MHHTTPService sharedInstance] currentUser];
    }
    return self;
}


- (void)initialize
{
    [super initialize];
    @weakify(self);
    self.title = @"设置";
     self.style = UITableViewStyleGrouped;
    self.footContentViewColor = MH_MAIN_BACKGROUNDCOLOR_2;
    self.keyboardEnable = NO;
    /// 获取网络数据+本地数据
//    RACSignal *fetchLocalDataSignal = [RACSignal return:[self fetchLocalData]];
//    RACSignal *requestRemoteDataSignal = self.requestRemoteDataCommand.executionSignals.switchToLatest;
//    [[[fetchLocalDataSignal
//       merge:requestRemoteDataSignal]
//      deliverOnMainThread]
//     subscribeNext:^(MHUser *user) {
//         @strongify(self)
//         [self willChangeValueForKey:@"user"];
//         /// user模型的数据 重置，但是user的 指针地址不变
//         [self.user mergeValuesForKeysFromModel:user];
//         [self didChangeValueForKey:@"user"];
//     }];
//    
    self.pushDetailInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        MHUserInfoViewModel *viewModel = [[MHUserInfoViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:[[MHHTTPService sharedInstance] currentUser]}];
        [self.services pushViewModel:viewModel animated:YES];
        return [RACSignal empty];
    }];
    
    /// 配置数据
    [self _configureData];

}

/// 获取本地的用户数据
- (MHUser *)fetchLocalData{
    return [[self.services client] currentUser];
}
/// 回去网络的用户数据 用于比对
- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    return [RACSignal empty];
}





#pragma mark - 配置数据
- (void)_configureData{
    
    /// 第一组
    MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
    /// 用户信息
    MHCommonProfileHeaderItemViewModel *profileHeader = [[MHCommonProfileHeaderItemViewModel alloc] initViewModelWithUser:self.user];
    /// 由于涉及到 更新用户的数据，这里最后把用户数据传递过去
//    profileHeader.destViewModelClass = [MHUserInfoViewModel class];
    @weakify(self);
    profileHeader.operation = ^{
        @strongify(self);
        MHUserInfoViewModel *viewModel = [[MHUserInfoViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:[[MHHTTPService sharedInstance] currentUser]}];
        [self.services pushViewModel:viewModel animated:YES];
    };
    profileHeader.rowHeight = 88.0f;
    group0.itemViewModels = @[profileHeader];
    
    /// 第一组
    MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
    
    MHCommonArrowItemViewModel *collection = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"收藏" icon:@"icon_message_collection"];
//    wallet.destViewModelClass = MHMyWalletViewModel.class;
    
   
    
    
    /// 钱包
    MHCommonArrowItemViewModel *wallet = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"我的钱包" icon:@"icon_mine_wallet"];
    wallet.destViewModelClass = MHMyWalletViewModel.class;
    /// 设置组头高度
   
    
   
    
    /// 第二组
//    MHCommonGroupViewModel *group2 = [MHCommonGroupViewModel groupViewModel];
//    /// 邀请
    MHCommonArrowItemViewModel *collect = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"推荐给朋友" icon:@"icon_mine_transfer"];
    collect.destViewModelClass = [MHShareAppViewModel class];
    /// 帮助与反馈
    //    MHCommonArrowItemViewModel *cardPackage = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"帮助与反馈" icon:@"MyCardPackageIcon_25x25"];
    //    /// 退出登录
    //    MHCommonArrowItemViewModel *expression = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"退出登录" icon:@"MoreExpressionShops_25x25"];
//    expression.destViewModelClass = [MHEmotionViewModel class];
//    group2.itemViewModels = @[collect];
    
    /// 第三组
//    MHCommonGroupViewModel *group3 = [MHCommonGroupViewModel groupViewModel];
    /// 设置
    MHCommonArrowItemViewModel *safe = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"账号与安全" icon:@"icon_mine_safe"];
    safe.destViewModelClass = MHAccountSecurityViewModel.class;

    
    MHCommonArrowItemViewModel *bindInvited = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"绑定邀请码" icon:@"icon_my_number"];
    bindInvited.destViewModelClass  = MHBindInvitedCodeViewModel.class;
    
    MHCommonGroupViewModel *group4 = [MHCommonGroupViewModel groupViewModel];
    MHCommonArrowItemViewModel *help = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"帮助与反馈" icon:@"icon_mine_help"];
    help.destViewModelClass = MHFeedBackViewModel.class;
    MHCommonArrowItemViewModel *aboutUs = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"关于我们" icon:@"icon_mine_aboutUs"];
    
    aboutUs.destViewModelClass = MHAboutUsViewModel.class;
    group4.itemViewModels = @[bindInvited,safe,help,aboutUs,collect];
    
    
    group1.itemViewModels = @[bindInvited,safe,help,aboutUs];
//    if([AXAppDelegate sharedDelegate].passAudit)
//    {
//        group1.itemViewModels = @[collection,wallet];
//    }else
//    {
//        group1.itemViewModels = @[collection];
//    }

//    group4.itemViewModels = @[];
    
    group1.footerHeight = 50;
//#else
//    /// 发布模式
//    group3.itemViewModels = @[setting];
//#endif
    
   
    
//     self.dataSource = @[group0,group1, group4];
    self.dataSource = @[group1];
    
    self.logoutCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        /// 删除账号
        [SAMKeychain deleteRawLogin];
        /// 先退出用户
        [[self.services client] logoutUser];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            /// 延迟一段时间
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                /// 这里切换 到账号登录的界面
                [MHNotificationCenter postNotificationName:MHSwitchRootViewControllerNotification object:nil userInfo:@{MHSwitchRootViewControllerUserInfoKey:@(MHSwitchRootViewControllerFromTypeLogout)}];
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
            });
            return [RACDisposable disposableWithBlock:^{
                
            }];
        }];
    }];
}
@end
